var Tables = Ext.extend(Ext.grid.GridPanel, {
	border : false,
	loadMask : true,
	initComponent : function() {
		var me = this;
		this.store = new Ext.data.JsonStore({
					fields : ['tablename', 'chinesename', 'keys'],
					url : My.url('/system/common/queryForList.do'),
					baseParams : {
						table : 't_tables',
						where : 'order by tablename',
						cols : '*'
					},
					root : 'data',
					autoLoad : true
				});
		this.sm = new Ext.grid.RowSelectionModel();
		this.cm = new Ext.grid.ColumnModel({
					columns : [new Ext.grid.RowNumberer(), {
								dataIndex : 'tablename',
								header : '表名',
								width : 300
							}, {
								dataIndex : 'chinesename',
								header : '中文名',
								width : 300
							}, {
								dataIndex : 'keys',
								header : '主键',
								width : 150
							}]
				});
		this.tbar = [{
			text : '同步',
			iconCls : 'icon-sync',
			handler : function() {
				My.request({
					url : My.url('/system/common/exec.do'),
					params : {
						sql : "begin\n"
								+ "  insert into t_tables\n"
								+ "    (tablename, keys)\n"
								+ "    select table_name,\n"
								+ "           (select wm_concat(COLUMN_NAME) keys\n"
								+ "              from user_cons_columns a\n"
								+ "              join (select constraint_name\n"
								+ "                     from user_constraints\n"
								+ "                    where constraint_type = 'P') b\n"
								+ "                on a.constraint_name = b.constraint_name\n"
								+ "             where table_name = t.table_name\n"
								+ "             group by a.table_name) keys\n"
								+ "      from (select table_name\n"
								+ "              from user_tables\n"
								+ "            minus\n"
								+ "            select tablename from t_tables) t;\n"
								+ "  delete t_tables\n"
								+ "   where tablename in (select tablename\n"
								+ "                         from t_tables\n"
								+ "                       minus\n"
								+ "                       select table_name from user_tables);\n"
								+ "end;"
					},
					successCallback : function() {
						me.getStore().reload();
					}
				});
			}
		}, {
			text : '修改',
			iconCls : 'icon-update',
			handler : function() {
				me.update();
			}
		}];
		Tables.superclass.initComponent.call(this);
	},
	update : function() {
		var me = this;
		var rec = this.getSelectionModel().getSelected();
		if (!rec) {
			TopMy.alert('请选择要修改的记录！');
			return;
		}
		var form = new TopExt.FormPanel({
					border : false,
					padding : 5,
					defaults : {
						xtype : 'textfield',
						anchor : '100%'
					},
					labelAlign : 'right',
					labelWidth : 70,
					items : [{
								name : 'tablename',
								fieldLabel : '表&emsp;&emsp;名',
								readOnly : true
							}, {
								name : 'chinesename',
								fieldLabel : '中&ensp;文&ensp;名',
								allowBlank : false
							}, {
								name : 'keys',
								fieldLabel : '主&emsp;&emsp;键'
							}],
					listeners : {
						afterrender : function(t) {
							t.getForm().loadRecord(rec);
						}
					}
				});
		var win = new TopExt.Window({
			title : '修改表信息',
			width : 380,
			autoHeight : true,
			items : [form],
			modal : true,
			buttons : [{
				text : '保存',
				iconCls : 'icon-save',
				handler : function() {
					TopMy.submit(form, {
						url : My.url('/system/common/exec.do'),
						params : {
							sql : "update t_tables set chinesename=#{chinesename},keys=#{keys} where tablename=#{tablename}"
						},
						successCallback : function() {
							me.getStore().reload();
							win.close();
						}
					});
				}
			}, {
				text : '取消',
				iconCls : 'icon-cancel',
				handler : function() {
					win.close();
				}
			}]
		});
		win.show();
	}
});
Ext.onReady(function() {
			new Ext.Viewport({
						layout : 'fit',
						items : [new Tables()]
					});
		});